package com.blockchain.front.servlets.trading.ajax;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.blockchain.framework.service.ServiceSession;
import com.blockchain.framework.service.exception.LogicalException;
import com.blockchain.framework.service.exception.ParameterException;
import com.blockchain.front.service.trading.TradingManage;
import com.blockchain.front.service.trading.entity.HqEntity;
import com.blockchain.front.servlets.trading.AbstractTradingServlet;
import com.blockchain.front.util.JsonUtil;
import com.blockchain.util.parser.IntegerParser;

public class Hq extends AbstractTradingServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void processPost(HttpServletRequest request, HttpServletResponse response, ServiceSession serviceSession)
			throws Throwable {
		PrintWriter out = response.getWriter();
		int scid = IntegerParser.parse(request.getParameter("scid"));
		Map<String,Object> result  = new HashMap<>();
		result.put("num", "0");
		result.put("msg", "");
		TradingManage tradingManage = serviceSession.getService(TradingManage.class);
		if (scid <= 0) {
			result.put("msg", "市场异常");
		out.write(JsonUtil.GsonString(result));
			return;
		}
		HqEntity xlb = tradingManage.getHq(scid);
		if (xlb != null) {
			result.put("num", "1");
			result.put("msg", xlb);
			out.write(JsonUtil.GsonString(result));
			return;
		}

	}

	@Override
	protected void onThrowable(HttpServletRequest request, HttpServletResponse response, Throwable throwable)
			throws ServletException, IOException {
		getResourceProvider().log(throwable.getMessage());
		if (throwable instanceof LogicalException || throwable instanceof ParameterException) {
			PrintWriter out = response.getWriter();
			StringBuilder sb = new StringBuilder();
			sb.append("[{'num':00,'msg':'");
			sb.append(throwable.getMessage() + "'}]");
			out.write(sb.toString());
		} else {
			super.onThrowable(request, response, throwable);
		}
	}
}
